package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService service;

    @GetMapping("/list")
    public R<List<AddressBook>> getAddressBookByUserId(HttpServletRequest request) {
        User loginUser=(User) request.getSession().getAttribute("userLogin");
        LambdaQueryWrapper<AddressBook> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, loginUser.getId());
        //默认地址置顶
        queryWrapper.orderByDesc(AddressBook::getIsDefault);
        List<AddressBook> list=service.list(queryWrapper);
        return R.success(list);
    }


    @PostMapping
    public R<String> addAddressBook(@RequestBody AddressBook addressBook, HttpServletRequest request) {
        //找出登录用户
        User loginUser=(User) request.getSession().getAttribute("userLogin");
        addressBook.setUserId(loginUser.getId());
        //判断账户下面是否存在默认地址
        //找出原默认地址
        LambdaQueryWrapper<AddressBook> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, loginUser.getId()).eq(AddressBook::getIsDefault, 1);
        AddressBook oldAddressBook=service.getOne(queryWrapper);
        //判断是否存在默认地址
        if (oldAddressBook == null) {
            //不存在 则把当前地址设置为 默认
            addressBook.setIsDefault(1);
        }
        boolean flag=service.save(addressBook);
        return flag ? R.success("添加成功!") : R.error("添加失败");
    }

    @GetMapping("/{id}")
    public R<AddressBook> getAddressById(@PathVariable Long id) {
        AddressBook addressBook=service.getById(id);

        return addressBook != null ? R.success(addressBook) : R.error("获取地址失败");
    }

    @PutMapping("/default")
    public R<AddressBook> changeUpdateDefaultAddressBook(@RequestBody AddressBook addressBook, HttpServletRequest request) {
        System.out.println(addressBook);
        User loginUser=(User) request.getSession().getAttribute("userLogin");
        addressBook.setUserId(loginUser.getId());
        AddressBook updateDefaultAddressBook=service.changeUpdateDefaultAddressBook(addressBook.getId(), loginUser.getId());
        return updateDefaultAddressBook !=null ? R.success(updateDefaultAddressBook) : R.error("修改默认地址失败!");
    }

    @GetMapping("/default")
    public R<AddressBook> getDefaultAddressBook(HttpSession session){
        User loginUser=(User)session.getAttribute("userLogin");
        AddressBook defaultAddressBook = service.getDefaultAddressBook(loginUser.getId());

        return defaultAddressBook!=null ? R.success(defaultAddressBook) : R.error("获取默认地址失败!");
    }


    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook, HttpServletRequest request) {
        User loginUser=(User) request.getSession().getAttribute("userLogin");
        addressBook.setUserId(loginUser.getId());
        boolean flag=service.updateById(addressBook);
        return flag ? R.success("修改成功!") : R.error("修改失败!");
    }

    @DeleteMapping
    public R<String> deleteAddressBook(Long ids) {
        Boolean flag=service.deleteAddressBook(ids);
        return flag ? R.success("刪除成功!") : R.error("刪除失败!");
    }

}
